const {
    async
} = require("../../utils/runtime")
// pages/addAddress/addAddress.js
var app = getApp()
var tableName = 'userAddress'
var keyId = ''
var userPhone = app.globalData.phone
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: ['广东省', '广州市', '海珠区'],
        customItem: '全部',
        addressArr: [],
        dizhivalue:''
    },
    submit: function (e) {
        if(this.data.id != 1 && this.data.id != 2 ){
            console.log(e)
            var addressee = e.detail.value.addressee
            var detailAddress = e.detail.value.detailAddress
            var phoneNumber = e.detail.value.phoneNumber
            var region = e.detail.value.region.join(' ')
            if(addressee==''|detailAddress==''|phoneNumber=='') {
            wx.showModal({
                title: '提示',
                content: '值不能为空',
                success (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              return
            }
            var arr = []
            var selected = '0'
            arr.push(addressee, detailAddress, phoneNumber, region, selected)
            this.data.addressArr.push(addressee, detailAddress, phoneNumber, region, selected)
            console.log(this.data.addressArr)
            if (keyId == '')
                this.setUserAddress(this.data.addressArr)
            else
                this.updateUserAddress(arr)
            console.log(addressee, detailAddress, phoneNumber, region)
            wx.redirectTo({
                url: '/pages/addresDetail/addresDetail',
              })
        }
        if(this.data.id == 1 || this.data.id == 2){
            var addressee = e.detail.value.addressee
            var detailAddress = e.detail.value.detailAddress
            var phoneNumber = e.detail.value.phoneNumber
            var region = e.detail.value.region.join(' ')
            if(addressee==''|detailAddress==''|phoneNumber=='') {
            wx.showModal({
                title: '提示',
                content: '值不能为空',
                success (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              return
            }
            var arr = []
            var selected = '0'
            arr.push(addressee, detailAddress, phoneNumber, region, selected)
            this.data.addressArr.push(addressee, detailAddress, phoneNumber, region, selected)
            console.log(this.data.addressArr)
            app.globalData.zhuzhi = e.detail.value.region
            app.globalData.zhuzhiaddress = this.data.addressArr[3] + this.data.addressArr[1]
            app.globalData.zhuzhifuwu = this.data.addressArr[1]
            wx.navigateBack({})
        }
       
    },
    RegionChange: function (e) {
        console.log(e)
        this.data.region = e.detail.value
        this.setData({
            region: this.data.region
        })
    },
    setUserAddress: function (e) {
        let tableName = 'userAddress'
        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.create()

        // 设置方式一
        let apple = {
            userPhone: app.globalData.phone,
            userAllAddress: e,
        }

        product.set(apple).save().then(res => {
            // success
            console.log(res)

        }, err => {
            //err 为 HError 对象
        })
        this.onLoad()
    },
    updateUserAddress: function (e) {
        let tableName = 'userAddress'
        let recordID = keyId // 数据行 id
        
        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.getWithoutData(recordID)
        
        product.set('userAllAddress', this.data.addressArr)
        product.update().then(res => {
          // success
        }, err => {
          // err
        })
        this.onLoad()
    },
    getUserAllAddress: function () {
        return new Promise((resolve, reject) => {
            let Product = new wx.BaaS.TableObject(tableName)
            let query = new wx.BaaS.Query()
            query.compare('userPhone', '=', userPhone)
            Product.setQuery(query).find().then(res => {
                // success
                console.log(res)
                resolve(res)
            }, err => {
                // err
                console.log('没有找到')
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        this.setData({
            id:options.id
        })
        if(this.data.id != 1 && this.data.id != 2 ){
            var resData = await this.getUserAllAddress()
            if (resData.data.objects[0].userAllAddress != null) {
                this.data.addressArr = resData.data.objects[0].userAllAddress
                keyId = resData.data.objects[0].id
            }
            console.log('数据', this.data.addressArr)
        }
        if(this.data.id == 2){
            this.setData({
                region:app.globalData.zhuzhi,
                dizhivalue:app.globalData.zhuzhifuwu
            })
            
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})